<template>
  <div class="Performancebox">
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="80px">
          <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="光伏系统">
                  <FilterSelect2
                      v-model="companyId"
                      :optionlist="xitonglist"
                      @updateModelValue="changexitong"
                      :valuekey="{ name: 'name', value: 'id' }"
                    ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="6" style="display:flex;margin-top: 4px;">
                <el-radio-group v-model="radio3" size="small" style="margin-right:20px ;">
                  <el-radio-button label="2">月</el-radio-button>
                  <el-radio-button label="3">年</el-radio-button>
                </el-radio-group>
                <el-date-picker style="flex:1;" v-if="radio3=='2'" v-model="value2" type="month" value-format="yyyy-MM" placeholder="选择月" size="small"></el-date-picker>
                <el-date-picker style="flex:1;" v-if="radio3=='3'" v-model="value3" type="year" value-format="yyyy" placeholder="选择年" size="small"></el-date-picker>
              </el-col>
              <el-col :span="13" style="text-align: right;">
                <el-form-item label="">
                  <el-button size="small" @click="chongzhi">重 置</el-button>
                  <el-button size="small" type="primary" @click="gettablelist">查 询</el-button>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="title">
        <div>性能分析</div>
        <div><TopTile  @command="command" @daochu="daochu" @shuaxin="shuaxin"></TopTile></div>
      </div>
      <div class="echandtab">
        <div class="tongbiech" ref="tongbiech"></div>
        <div class="tongbiech" >
          <TableColumn
            ref="tabledata"
            :tableData="tableData" 
            :tableConfig="tableConfigs" 
            :issize="issize">
          </TableColumn>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import { exportexcel } from '@/utils/excel'
import { getphotovollistapi } from '@/api/Photovoltaic/analysis/YoyAnalysis'
import {getxingnnegquxianapi,getxingnnegtableapi} from '@/api/Photovoltaic/analysis/Performance'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn
  },
  data() {
    return {
      loading: false,
      issize: 'medium',
      value3: '',
      value2:'',
      radio3:'2',
      companyId:'',
      xitonglist: [],
      tableData: [],
      tableConfigs: [
      { label: '序号', type: 'index', center: 'center', width: '70' },
        { label: '日期', prop: 'dateTime' , center: 'center'},
        { label: '辐照度(W/m³)', prop: 'irradiance', center: 'right'  },
        { label: '理论发电量(kWh)', prop: 'theoreticalQuantity', center: 'right'  },
        { label: '实际发电量(kWh)', prop: 'actualQuantity', center: 'right'},
        { label: '峰值日照时数(h)', prop: 'peakSunshineHours',center: 'right' },
        { label: '等效小时(h)', prop: 'equivalentHours', center: 'right' },
        { label: '性能(%)', prop: 'performance', center: 'right' },
      ]
    };
  },

  async mounted() {
    this.gettime()
    const { data: { list } } = await getphotovollistapi()
    this.xitonglist = list
    const PhotoCompanyId = localStorage.getItem('PhotoCompanyId')
    this.$set(this, 'companyId', PhotoCompanyId ? +PhotoCompanyId : list[0].id)
    this.gettablelist()
  },

  methods: {
    async gettablelist() {
      this.$refs.tabledata.loading=true
      const { data: { list } } = await getxingnnegtableapi({
        companyId:this.companyId,dateString:this.radio3=='2'?this.value2:this.value3,dateType:this.radio3
      })
      this.tableData=list
      this.$refs.tabledata.loading=false
      this.getfadianlv()
    },
    // 系统下拉存本地
    changexitong(val) {
      localStorage.setItem('PhotoCompanyId', val)
    },
    // 获取电量图表
    async getfadianlv() {
      var chartDom = this.$refs.tongbiech
      var myChart = echarts.init(chartDom);
      var option;
      const loading = this.$loading({
        lock: true,
        text: 'loading',
        spinner: 'el-icon-loading',
        target: chartDom
      });
      const { data: { list } } = await getxingnnegquxianapi({
        companyId:this.companyId,dateString:this.radio3=='2'?this.value2:this.value3,dateType:this.radio3
      })
      const timePoints =[]
      for (let i = 0; i < list.length; i++) {
          if (list[i].datalist.length > 0) {
              list[i].datalist.forEach(data => {
                timePoints.push(data.timeReading);
              });
              break;
          }
      }
      option = {
        color:['#5291ff','#52ffff','#f3b53b'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        grid: {
          left: '2%',
          right: '1%',
          bottom: '3%',
          containLabel: true
        },
        legend: {},
        xAxis: [
          {
            type: 'category',
            data: timePoints,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '电量(kWh)',
            axisLine: {
              show: true
            }
          },
          {
            type: 'value',
            name: 'PR(%)',
            axisLine: {
              show: true
            }
          }
        ],
        series: [
          {
            name: '实际发电量',
            type: 'bar',
            data: list.filter(ele=>ele.name=='实际发电量')[0].datalist.map(ele=>ele.valueReading)
          },
          {
            name: '理论发电量',
            type: 'bar',
            data: list.filter(ele=>ele.name=='理论发电量')[0].datalist.map(ele=>ele.valueReading)
          },
          {
            name: 'Pr值',
            type: 'line',
            smooth: true,
            yAxisIndex: 1,
            data:list.filter(ele=>ele.name=='Pr值')[0].datalist.map(ele=>ele.valueReading)
          }
        ]
      };
      myChart.clear();
      option && myChart.setOption(option);
      loading.close()
    },
    // 重置
    chongzhi() {
      this.radio3 = '2'
      const PhotoCompanyId = localStorage.getItem('PhotoCompanyId')
      this.$set(this, 'companyId', PhotoCompanyId ? +PhotoCompanyId : this.xitonglist[0].id)
      this.gettime()
      this.gettablelist()
    },
    // 调节表格密度
    command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 导出
    daochu() {
      exportexcel('pes-microservice-biz/pvm/performance/analyse/export',{
        companyId:this.companyId,dateString:this.radio3=='2'?this.value2:this.value3,dateType:this.radio3
      },'性能分析')
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    },
    // 获取当前时间戳
    gettime() {
      const date = new Date()
      const year = date.getFullYear() // 年
      const mon = date.getMonth() + 1 // 月
      const month = mon < 10 ? '0' + mon : mon
      this.$set(this,'value2',year+'-'+month)
      this.$set(this,'value3',year.toString())
    },
  },
};
</script>

<style scoped>
.Performancebox{
  height: 100%;
  padding: 10px;
  /* color: #f3b53b; */
  .topbox{
    height: 80px;
    background-color: #fff;
    padding:  20px;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    background-color: #fff;
    padding: 20px;
    .title{
      margin-bottom: 10px;
      display:flex;
      justify-content:space-between;
    }
    .echandtab{
      height: 95%;
      .tongbiech{
        height: 50%;
      }
    }
  }
}
</style>